package kr.ac.hanyang.cs.park;

import java.io.ByteArrayOutputStream;
import kr.ac.hanyang.cs.park.DBConstants.Task;
import android.app.Activity;
import android.content.ContentValues;
import android.database.sqlite.SQLiteDatabase;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.drawable.Drawable;
import android.media.AudioFormat;
import android.media.AudioRecord;
import android.media.MediaRecorder;
import android.os.Bundle;
import android.os.Environment;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.ProgressBar;


public class RegisterTask extends Activity {
	private TaskDBHelper taskDBHelper					= null;
	private SQLiteDatabase taskDB						= null;

	
	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
	    super.onCreate(savedInstanceState);
	    setContentView(R.layout.registertask);
	
	    // TODO Auto-generated method stub
	    taskDBHelper									= new TaskDBHelper(this);
	    

	    // TODO: Voice Command Register
	    ProgressBar barProgress							= (ProgressBar) findViewById(R.id.register_barProgress);
	    Button btnRec									= (Button) findViewById(R.id.register_btnRecCommand);
	    Button btnPlay									= (Button) findViewById(R.id.register_btnPlayCommand);

	    barProgress.setEnabled(false);
	    btnRec.setEnabled(false);
	    btnPlay.setEnabled(false);
	    // END OF VOICE COMMAND REGISTER
	    
	    
	    Button btnRegister								= (Button) findViewById(R.id.register_btnRegisterTask);
	    btnRegister.setOnClickListener(registerBtnClickListener);

	}



	private Button.OnClickListener registerBtnClickListener = new Button.OnClickListener() {
		public void onClick(View v) {
			// get task object
			ParcelableTask receivedTask					= getIntent().getExtras().getParcelable("TASK");
			TaskInfo task								= receivedTask.getTask();

			// extract informations from task
			String taskName								= task.getTaskName();
			String taskType								= task.getTaskType().toString();
			String taskOption							= task.getTaskOption().toString();
			String taskDescription						= task.getDescription().toString();
			Drawable taskIcon							= task.getIcon();

			// insert informations into db
			taskDB										= taskDBHelper.getWritableDatabase();
			ContentValues values						= new ContentValues();

			// convert Drawable object to ByteArrayOutputStream object, to save into sqlite blob field
			ByteArrayOutputStream baos					= new ByteArrayOutputStream();  
			Bitmap bitmap								= Bitmap.createBitmap(150, 150, Bitmap.Config.ARGB_8888);
			Canvas canvas								= new Canvas(bitmap);
			taskIcon.setBounds(0, 0, 150, 150);
			taskIcon.draw(canvas);
			bitmap.compress(Bitmap.CompressFormat.PNG, 100, baos);   
			byte[] taskIconToSave						= baos.toByteArray(); 

			values.put(Task.TASKNAME, taskName);
			values.put(Task.TASKTYPE, taskType);
			values.put(Task.TASKICON, taskIconToSave);
			values.put(Task.TASKOPTION, taskOption);
			values.put(Task.TASKDESCRIPTION, taskDescription);

			taskDB.insert(Task.TABLE_NAME, null, values);

			taskDB.close();
		    taskDBHelper.close();

			finish();
		}
		
	};
	
	
}
